//
//  SEGNewBusinessCircleDetailsViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/28.
//

#import "SEGNewBusinessCircleDetailsViewModel.h"
#import "SEGMerchantShopModel.h"

@interface SEGNewBusinessCircleDetailsViewModel ()
@property (nonatomic, strong) NSString *sid;

@end

@implementation SEGNewBusinessCircleDetailsViewModel
#pragma mark - life cycle
- (instancetype)initWithSid:(NSString *)sid {
    if (self = [super init]) {
        _sid = sid;
        [self seg_creatListDataCommand];
    }
    return self;
}

- (void)dealloc {
    
}

#pragma mark - init
- (void)seg_creatListDataCommand {
    @weakify(self);
    self.listCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            if (!self.sid) {
                [subscriber sendCompleted];
                return nil;
            }
            self.sid = ValueToString(self.sid);
            if (self.sid.length == 0) {
                [subscriber sendCompleted];
                return nil;
            }
            
            WeakSelf;
            [BusinessCircleRequestManager requestMerchantDetailWithMerchantSid:self.sid finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                NSDictionary* dict = result;
                //数据返回正常
                if (isSuccessed) {
                    NSDictionary *data = [dict objectForKey:@"data"];
                    self.model = [SEGNewBusinessCircleDetailsModel yy_modelWithDictionary:data];
                    
                    NSString *pic1 = ValueToString([data objectForKey:@"pic1"]);
                    NSString *pic2 = ValueToString([data objectForKey:@"pic2"]);
                    NSString *pic3 = ValueToString([data objectForKey:@"pic3"]);
                    NSString *pic4 = ValueToString([data objectForKey:@"pic4"]);
                    NSString *pic5 = ValueToString([data objectForKey:@"pic5"]);
                    NSMutableArray *tempImageArray = [NSMutableArray array];
                    if (pic1.length > 0) {
                        [tempImageArray addObject:pic1];
                    }
                    if (pic2.length > 0) {
                        [tempImageArray addObject:pic2];
                    }
                    if (pic3.length > 0) {
                        [tempImageArray addObject:pic3];
                    }
                    if (pic4.length > 0) {
                        [tempImageArray addObject:pic4];
                    }
                    if (pic5.length > 0) {
                        [tempImageArray addObject:pic5];
                    }
                    
                    weakSelf.imagesArray = [tempImageArray copy];
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
            }];
            
            
            return nil;
        }];
    }];
}

#pragma mark - send request

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods

#pragma mark - setter and getter
@end
